package com.risen.project.common;

import java.time.*;
import java.time.temporal.TemporalAdjusters;

/**
 * 时间工具类
 */
public class TimeUtil {
    /**
     * 获取LocalDateTime的时间戳
     */
    public static long localDateTimeTimestamp(LocalDateTime time) {
        return time.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 时间戳转LocalDateTime
     */
    public static LocalDateTime timestampToLocalDateTime(long timestamp) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
    }

    /**
     * 是获取前后n天的日期
     */
    public static LocalDate beforeOrAfterDay(int n) {
        return LocalDate.now().plusDays(n);
    }

    /**
     * 获取当天的开始时间
     */
    public static LocalDateTime dayStartTime() {
        return LocalDateTime.of(LocalDate.now(), mini());
    }

    /**
     * 获取指定日期的开始时间
     */
    public static LocalDateTime dayStartTime(LocalDate date) {
        return LocalDateTime.of(date, mini());
    }

    /**
     * 获取当天的结束时间
     */
    public static LocalDateTime dayEndTime() {
        return LocalDateTime.of(LocalDate.now(), max());
    }

    public static LocalDateTime dayEndTime(LocalDate date) {
        return LocalDateTime.of(date, max());
    }

    /**
     * 获取本周第一天的开始时间
     */
    public static LocalDateTime weekStartTime() {
        return LocalDateTime.of(LocalDate.now().with(DayOfWeek.MONDAY), mini());
    }

    public static LocalDateTime weekStartTime(LocalDate date) {
        return LocalDateTime.of(date.with(DayOfWeek.MONDAY), mini());
    }

    /**
     * 获取本周最后一天的结束时间
     */
    public static LocalDateTime weekEndTime() {
        return LocalDateTime.of(LocalDate.now().with(DayOfWeek.SUNDAY), max());
    }

    public static LocalDateTime weekEndTime(LocalDate date) {
        return LocalDateTime.of(date.with(DayOfWeek.SUNDAY), LocalTime.MAX);
    }

    /**
     * 获取本月第一天的开始时间
     */
    public static LocalDateTime getMonthStartTime() {
        return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()), mini());
    }

    public static LocalDateTime getMonthStartTime(LocalDate date) {
        return LocalDateTime.of(date.with(TemporalAdjusters.firstDayOfMonth()), mini());
    }

    /**
     * 获取当月结束当天的结束时间
     */
    public static LocalDateTime getMonthEndTime() {
        return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()), max());
    }

    public static LocalDateTime getMonthEndTime(LocalDate date) {
        return LocalDateTime.of(date.with(TemporalAdjusters.lastDayOfMonth()), max());
    }

    public static LocalDateTime startDayOfYear() {
        return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfYear()), mini());
    }

    public static LocalDateTime endDayOfYear() {
        return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfYear()), max());
    }

    private static LocalTime max() {
        return LocalTime.of(23, 59, 59);
    }

    private static LocalTime mini() {
        return LocalTime.of(0, 0, 1);
    }
}
